package com.linxz.framework.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import java.util.LinkedList;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * @author Linxz
 * 创建日期：2019年02月20日 19:48
 * version：1.0.0
 * 描述：
 */
public class AppActivityManager {
    private static LinkedList<Activity> acts=new LinkedList<>();
    private Map<String,Object> params;

    private AppActivityManager() {
    }
    private AppActivityManager(Map<String,Object> params){
        this.params=params;
    }

    public static AppActivityManager getInstance() {
        return new AppActivityManager();
    }

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder{
        private Map<String,Object> params=new WeakHashMap<>();
        public Builder withString(String key,String val){
            params.put(key,val);
            return this;
        }
        public Builder withInt(String key,Integer val){
            params.put(key,val);
            return this;
        }
        public Builder withBolean(String key,Boolean val){
            params.put(key,val);
            return this;
        }
        public AppActivityManager build(){
            return new AppActivityManager(params);
        }
    }

    public void goTo(Activity self, Class<? extends Activity> cls) {
        Log.d("LaunchTimeAspect","页面挑战的时候");
        Intent it = new Intent(self, cls);
        self.startActivity(it);
    }

    public void goTo(Activity self, Intent it) {
        Log.d("LaunchTimeAspect","页面挑战的时候");
        self.startActivity(it);
    }

    public void goTo(Context self, Intent it) {
        Log.d("LaunchTimeAspect","页面挑战的时候");
        self.startActivity(it);
    }

    public void goTo(Context self, Class<? extends Activity> cls) {
        Log.d("LaunchTimeAspect","页面挑战的时候");
        Intent it = new Intent(self, cls);
        self.startActivity(it);
    }


    public void goFoResult(Activity self, Class<? extends Activity> cls, int RequestCode) {
        Intent it = new Intent(self, cls);
        goFoResult(self, it, RequestCode);
    }

    public void goFoResult(Activity self, Intent it, int RequestCode) {
        self.startActivityForResult(it, RequestCode);
    }

    public void goFoResult(Fragment self, Intent it, int RequestCode) {
        self.startActivityForResult(it, RequestCode);
    }

    public void goFoResultBottom(Activity self, Class<? extends Activity> cls, int RequestCode) {
        Intent it = new Intent(self, cls);
        goFoResultBottom(self, it, RequestCode);
    }

    public void goFoResultBottom(Activity self, Intent it, int RequestCode) {
        self.startActivityForResult(it, RequestCode);
        // self.overridePendingTransition(R.anim.dock_bottom_enter, R.anim.dock_bottom_exit);
    }

    /**组件化通过阿里路由跳转页面*/
    public void goTo(String scheme){
        Postcard postcard= ARouter.getInstance().build(scheme);
        if (params!=null){
            for (Map.Entry<String,Object> entry:params.entrySet()){
                Object val=entry.getValue();
                if (val instanceof String){
                    postcard.withString(entry.getKey(),entry.getValue().toString());
                }else if (val instanceof Integer){
                    postcard.withInt(entry.getKey(),Integer.parseInt(val+""));
                }else if (val instanceof Boolean){
                    postcard.withBoolean(entry.getKey(),Boolean.valueOf(val+""));
                }

            }
        }
        postcard.navigation();
    }


    public void addActivity(Activity act) {
        acts.add(act);
    }

    public void removeActivity(Activity act) {
        if (acts != null && acts.indexOf(act) >= 0) {
            acts.remove(act);
        }
    }

    public void cleanActivity() {
        while (acts.size() != 0) {
            Activity act = acts.poll();
            act.finish();
        }
    }


    public Activity getTopActivity() {
        return (acts == null || acts.size() <= 0) ? null : acts.get(acts.size() - 1);
    }

    public  LinkedList<Activity> getActs(){
        return acts;
    }

    /**
     *    1.关闭推送信息
     *    2.清空全部缓存信息
     *    3.退出全部activity
     *    4.进入登录页面
     * */
    public void quit(BaseActivity context) {
        //关闭推送
        //        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //        nm.cancelAll();
        //        // 清除临时文件
        //        File temppraryFile = new File(Constant.TEMPORARY_FILE_PATH);
        //        if (temppraryFile.exists()){
        //            UIHelper.deleteAllFiles(temppraryFile);
        //        }
        //        //清空Shareprefrence文件
        //        SharedPreferenceUtil.clearCookie(context);
        //        while (acts.size() != 0) {
        //            Activity act = acts.poll();
        //            if (act!=context){
        //                act.finish();
        //            }
        //        }
        //        LoginActivity.luanch(context);
        context.finish();
    }

    /**退出应用*/
    public void exit(){
        cleanActivity();
        System.exit(0);
    }
}
